﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using Project5188.BUS;
using Project5188.BUS.EducationAndTraining;
using ExportToExcelTools;
using Project5188.Client.ClientCodes;

namespace Project5188.Client.EducationAndTraining
{
    /// <summary>
    /// Interaction logic for CoursePage.xaml
    /// </summary>
    public partial class CoursePage
    {
        private CollectionViewSource _resources;

        public CoursePage()
            : base(Extensions.PageNames.CoursePage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;
        }

        void PagingControlDataChanged(object sender, EventArgs e)
        {
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgCourses.ItemsSource = _resources.Source as IEnumerable;
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            var insert = new InsertWindow();
            insert.Closed += InsertClosed;
            DetailView<course> courseDetail = new Courses();
            insert.SetEntity(courseDetail, Extensions.PageNames.CoursePage.ToString());
            insert.Show();
        }

        public override bool Search()
        {
            if (base.Search())
            {
                var courses = new Courses();
                _resources = Resources["coursesViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(courses.GetEntityListByParameters(txtCourseName.Text), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgCourses.ItemsSource = _resources.Source as IEnumerable;
            }
            else if (TransactionInfo.TransactionId.HasValue)
            {
                var courses = new Courses();
                _resources = Resources["coursesViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(courses.GetEntityById(TransactionInfo.TransactionId.Value), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgCourses.ItemsSource = _resources.Source as IEnumerable;
            }

            return true;
        }

        private void BtnDetailClick(object sender, RoutedEventArgs e)
        {
            var courseValue = (course)dgCourses.SelectedItem;
            var detail = new DetailWindow();
            DetailView<course> courseValueDetail = new Courses { Entity = courseValue };
            detail.SetEntity(courseValueDetail, Extensions.PageNames.CoursePage.ToString());
            detail.Show();
        }

        private void BtnUpdateClick(object sender, RoutedEventArgs e)
        {
            var courseValue = (course)dgCourses.SelectedItem;
            var update = new UpdateWindow();
            update.Closed += UpdateClosed;
            DetailView<course> courseValueDetail = new Courses();
            update.SetEntity(courseValueDetail, Extensions.PageNames.CoursePage.ToString(), courseValue.a_ID);
            update.Show();
        }

        private void BtnDeleteClick(object sender, RoutedEventArgs e)
        {
            var courseValue = (course)dgCourses.SelectedItem;
            DetailView<course> courseValueDetail = new Courses();
            GenerateDeleteDialog(courseValueDetail, courseValue.a_ID);
        }

        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            try
            {
                dgCourses.ExportToExcel();
            }
            catch
            {
                MessageBox.Show("Excel'e aktarma sırasında hata oluştu.");
            }
        }
    }
}
